package com.bank.backend.boot.server.controller;


import com.bank.backend.boot.base.model.dto.BankUserDTO;
import com.bank.backend.boot.base.model.response.Result;
import com.bank.backend.boot.base.model.vo.TokenVO;
import com.bank.backend.boot.server.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户登录注册
 *
 * @author jiaowei
 * @since 2025/3/23 19:45
 */
@Slf4j
@Tag(name = "用户登录注册")
@RestController
@RequestMapping("/first")
public class LoginController {
    @Resource
    private LoginService loginService;

    /**
     * 注册
     * @return
     */
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody BankUserDTO bankUserDto) {
        log.warn("银行用户注册信息入参：{}", bankUserDto);
        try {
            loginService.register(bankUserDto);
            return Result.success();
        } catch (Exception e) {
            log.warn("银行用户注册异常：{}", e);
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody BankUserDTO userDto) {
        log.warn("UserDto:{}", userDto);
        try {
            TokenVO tokenVo = loginService.login(userDto);
            return Result.success(tokenVo);
        } catch (Exception e) {
            log.warn("用户登录异常：{}", e);
            return Result.error(e.getMessage());
        }
    }

}
